import { Module } from '@nestjs/common';
import { ConfigModule, ConfigService } from '@nestjs/config';
import { MongooseModule } from '@nestjs/mongoose';
import { UserModule } from 'src/user/user.module';
import { WorkModule } from 'src/work/work.module';
import { UtilsModule } from 'src/utils/utils.module';
import { AppController } from './app.controller';
import { AppService } from './app.service';
import { CustomLoggerProvider } from './common/provider/logger.provider';
import { AuthModule } from './auth/auth.module';
import { RedisModule } from './common/redis/redis.module';
import { CaslModule } from './casl/casl.module';
import configuration from './config/configuration';

@Module({
  imports: [
    ConfigModule.forRoot({
      load: [configuration],
      isGlobal: true,
      envFilePath: [`.env.${process.env.NODE_ENV}`, '.env'],
    }),
    MongooseModule.forRootAsync({
      inject: [ConfigService],
      useFactory: (configService: ConfigService) => ({
        uri: configService.get('database.mongoUrl'),
        auth: {
          username: configService.get('database.mongoUsername'),
          password: configService.get('database.mongoPassword'),
        },
        authSource: configService.get('database.authSource'),
      }),
    }),
    UserModule,
    WorkModule,
    UtilsModule,
    AuthModule,
    RedisModule,
    CaslModule,
  ],
  controllers: [AppController],
  providers: [AppService, CustomLoggerProvider],
})
export class AppModule {}
